package com.emotion.recognition.client;

import com.emotion.recognition.shared.Constants;
import com.emotion.recognition.shared.Emotion;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class EditPanel extends Composite {

	private static EditPanelUiBinder uiBinder = GWT
			.create(EditPanelUiBinder.class);

	interface EditPanelUiBinder extends UiBinder<Widget, EditPanel> {
	}

	@UiField
	VerticalPanel verticalPanel;

	@UiField
	VerticalPanel radioEmotionPanel;

	@UiField
	RadioButton astonishedButton;

	@UiField
	RadioButton sadButton;

	@UiField
	RadioButton calmButton;

	@UiField
	RadioButton smilingButton;

	private Image imgToCrop = new Image();
	private boolean isTraining = true;
	private final String[] testingFileNames, trainingFileNames;
	private int numberCropped = 0;
	private final Emotion[] testingEmotions;

	public EditPanel(String fileNames) {

        trainingFileNames = fileNames.split(";")[0].split(",");
        testingFileNames = fileNames.split(";")[1].split(",");
		testingEmotions = new Emotion[trainingFileNames.length];
		initWidget(uiBinder.createAndBindUi(this));
	}

	// Returns true if next image exists. Returns false if reached the end.
    public int nextImage() {
        int retval = 1;
		// Get previous image's data, if applicable
		if ((numberCropped > 0) && isTraining) {
			testingEmotions[numberCropped - 1] = getEmotionByChecked();
		}

		// Check to see which stage we're in
		if (isTraining && numberCropped == trainingFileNames.length) {
			// Training is done, move on to testing images
			isTraining = false;
			numberCropped = 0;
            retval = 2; // 2 is switching over to testing
		} else if (!isTraining && numberCropped == testingFileNames.length) {
			// Testing is done, we are finished.
			verticalPanel.clear();
            return 0;
		}

		// Check if we're in the training stage
		if (!isTraining) {
			radioEmotionPanel.setVisible(false);
		}

		// Clear last image
		verticalPanel.clear();
		imgToCrop = new Image();
		verticalPanel.add(imgToCrop);

		// Load next image
		imgToCrop.setUrl(!isTraining ? Constants.TESTING_DIRECTORY_CLIENT + "/"
				+ testingFileNames[numberCropped]
				: Constants.TRAINING_DIRECTORY_CLIENT + "/"
						+ trainingFileNames[numberCropped]);

		// Attribute for JCrop.
		DOM.setElementAttribute(imgToCrop.getElement(), "id", "imgToJCrop");

		// On to the next image
		numberCropped++;

        return retval;
	}

	public Emotion[] getTestingEmotions() {
		return testingEmotions;
	}

	private Emotion getEmotionByChecked() {
		if (astonishedButton.getValue()) {
			return Emotion.ASTONISHED;
		} else if (calmButton.getValue()) {
			return Emotion.CALM;
		} else if (sadButton.getValue()) {
			return Emotion.SAD;
		} else {
			return Emotion.SMILING;
		}
	}
}
